/**
 * @description form表单的校验规则
 */
import isMobilePhone from 'validator/lib/isMobilePhone'
// import isIdentityCard from 'validator/lib/isIdentityCard'
import isEmail from 'validator/lib/isEmail'

/**
 * @description 多个字段翻译拼接的段落
 */
export const zhAndEnShowMsg = function () {
  const arr = Array.prototype.slice.call(arguments)
  return g_i18n.global.locale.value === 'en' ? arr.join('\t') : arr.join('')
}

/**
 * 公共文本输入框的校验
 * 非空限制
 * @param {*} label
 * @returns Object
 */
export const commonInputText = (label = '', required = true) => {
  return [
    {
      required,
      get errorMessage() {
        return zhAndEnShowMsg(g_t('public.pleaseInput'), label)
      },
      trigger: ['blur', 'change']
    }
  ]
}

// 通用单行文本输入框的校验
export const singleLineText = (label = '') => {
  return [
    ...commonInputText(label),
    {
      pattern: '^[\\u4e00-\\u9fa5a-zA-Z0-9-_]{1,128}$',
      get errorMessage() {
        return g_t('rule.chineseLetterNumTips', { max: 128 })
      },
      trigger: ['blur', 'change']
    }
  ]
}

// 多行文本输入框的校验
export const multilineText = (label = '') => {
  return [
    ...commonInputText(label),
    {
      pattern: '^.{1,128}$',
      get errorMessage() {
        return g_t('rule.charLimitTips', { max: 128 })
      },
      trigger: ['blur', 'change']
    }
  ]
}

// 下拉框的校验
export const selectRule = (label = '') => {
  return [
    {
      required: true,
      get errorMessage() {
        return zhAndEnShowMsg(g_t('public.pleaseSelect'), label)
      },
      trigger: ['blur', 'change']
    }
  ]
}

// 上传文件的校验
export const uploadFileRule = (label = '') => {
  return [
    {
      required: true,
      get errorMessage() {
        return zhAndEnShowMsg(g_t('public.pleaseUpload'), label)
      },
      trigger: 'change'
    }
  ]
}

// 创建数字类型验证规则工厂
const createNumberRule = (label, errorMsg, required = true) => [
  ...commonInputText(label, required),
  {
    pattern: '^[1-9]\\d*$',
    get errorMessage() {
      return errorMsg
    },
    trigger: ['blur', 'change']
  }
]

// 创建数字或小数点类型验证规则工厂
const createDecimalRule = (label, errorMsg, required = true) => [
  ...commonInputText(label, required),
  {
    // 只能输入正整数或者小数，不能以0开头的正整数，可以是一个0开头的小数，不能只输入一个0，不能只输入空字符串或多个空字符串
    // pattern: /^(0|[1-9]\d*)(\.\d+)?$/,
    pattern:
      '^(?!0$)(?!0\\d)(?!0\\.0$)(?!\\s*$)([1-9]\\d*(\\.\\d+)?|0\\.\\d*[1-9]\\d*)$',
    get errorMessage() {
      return errorMsg
    },
    trigger: ['blur', 'change']
  }
]

// 创建零或数字或小数点类型验证规则工厂
const createDecimalRule2 = (label, errorMsg, required = true) => [
  ...commonInputText(label, required),
  {
    // 只能输入正整数或者小数，不能以0开头的正整数，可以是一个0开头的小数，可以输入一个0，不能只输入空字符串或多个空字符串
    pattern: '^(0|[1-9]\\d*)(\\.\\d*[1-9]\\d*|(\\.0+)?)$',
    get errorMessage() {
      return errorMsg
    },
    trigger: ['blur', 'change']
  }
]

// 用户名
export const userName = [
  ...commonInputText('用户名'),
  {
    pattern: '^\\w{1,30}$',
    get errorMessage() {
      return g_t('rule.letterNumTips', { max: 30 })
    },
    trigger: ['blur', 'change']
  }
]

// 联系电话
export const phone = ({
  label = g_t('system.phone'),
  required = true
} = {}) => {
  return [
    ...commonInputText(label, required),
    {
      validateFunction(rule, value, data, callback) {
        // 没填不需要校验，填了需校验
        if (!rule.required && !value.length) {
          callback()
        } else if (isMobilePhone(value, 'zh-CN')) {
          callback()
        } else {
          callback(g_t('rule.validatePhone'))
        }
        return true
      },
      trigger: ['blur', 'change']
    }
  ]
}

// 邮箱
export const email = ({
  label = g_t('system.email'),
  required = true
} = {}) => {
  return [
    ...commonInputText(label, required),
    {
      validateFunction(rule, value, data, callback) {
        // 没填不需要校验，填了需校验
        if (!rule.required && !value.length) {
          callback()
        } else if (isEmail(value)) {
          callback()
        } else {
          callback(g_t('rule.validateEmail'))
        }
      },
      trigger: ['blur', 'change']
    }
  ]
}

// 密码
export const password = [
  ...commonInputText('登录密码'),
  {
    pattern: `^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d!@#$%^&*()_+\\-=\\[\\]{};:'",./<>?]{8,30}$`,
    get errorMessage() {
      return g_t('rule.passwordRequire', { min: 8, max: 30 })
    },
    trigger: ['blur', 'change']
  }
]

// 确认密码
export const confirmPassword = key => {
  return [
    ...commonInputText(g_t('system.confirmPassword')),
    {
      pattern: `^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d!@#$%^&*()_+\\-=\\[\\]{};:'",./<>?]{8,30}$`,
      get errorMessage() {
        return g_t('rule.passwordRequire', { min: 8, max: 30 })
      },
      trigger: ['blur', 'change']
    },
    {
      validateFunction(rule, value, data, callback) {
        if (data[key] === value) {
          callback()
        } else {
          callback(g_t('rule.confirmPassword'))
        }
      },
      trigger: ['blur', 'change']
    }
  ]
}

// 投放数量
export const putNumber = [
  ...commonInputText(g_t('system.putNumber')),
  {
    pattern: '^[1-9]\\d{1,3}$',
    get errorMessage() {
      return g_t('rule.numberTips', { max: 4 })
    },
    trigger: ['blur', 'change']
  }
]

// 烹饪份数
export const cookingServing = [
  ...commonInputText(g_t('recipe.cookingServing')),
  {
    pattern: '^([1-9]|10)$',
    get errorMessage() {
      return '烹饪份数限定最大值为10'
    },
    trigger: ['blur', 'change']
  }
]

// 菜谱步骤中的重量
export const stepGoal = (label = '食材重量') => {
  return [
    ...commonInputText(label),
    {
      validateFunction(rule, value, data, callback) {
        const num = Number(value)
        if (num > 0 && num <= 10000 && !isNaN(num)) {
          callback()
        } else {
          callback('重量不超过10000克（10kg）')
        }
      },
      trigger: ['blur', 'change']
    }
  ]
}

// 翻倒次数
export const turnCount = [
  ...commonInputText('翻倒次数'),
  {
    pattern: '^([1-3])$',
    get errorMessage() {
      return '翻倒次数必须在1到3次之间'
    },
    trigger: ['blur', 'change']
  }
]

// 调料盒中的调料余量
export const ingredientRemain = (label = '调料余量', type) => {
  return [
    ...commonInputText(label),
    {
      validateFunction(rule, value, data, callback) {
        const num = Number(value)
        const limit = type === 0 ? 1000 : 2000
        if (num > 0 && num <= limit && !isNaN(num)) {
          callback()
        } else {
          callback(type === 0 ? '重量不超过1000克（1kg）' : '重量不超过2000ml')
        }
      },
      trigger: ['blur', 'change']
    }
  ]
}
